package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.FSchedueTemplate;
import com.cookor.poi.modules.phy.service.FSchedueTemplateService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/fSchedueTemplate"})
public class FSchedueTemplateController extends BaseController {
  @Autowired
  private FSchedueTemplateService fSchedueTemplateService;
  
  @ModelAttribute
  public FSchedueTemplate get(@RequestParam(required = false) String id) {
    FSchedueTemplate entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.fSchedueTemplateService.get(id); 
    if (entity == null)
      entity = new FSchedueTemplate(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:fSchedueTemplate:view"})
  @RequestMapping({"list", ""})
  public String list(FSchedueTemplate fSchedueTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
    Page<FSchedueTemplate> page = this.fSchedueTemplateService.findPage(new Page(request, response), fSchedueTemplate);
    model.addAttribute("page", page);
    model.addAttribute("fSchedueTemplate", fSchedueTemplate);
    return "modules/phy/fSchedueTemplateList";
  }
  
  @RequiresPermissions({"phy:fSchedueTemplate:view"})
  @RequestMapping({"form"})
  public String form(FSchedueTemplate fSchedueTemplate, Model model) {
    if (StringUtils.isBlank(fSchedueTemplate.getId())) {
      fSchedueTemplate.setMon(Integer.valueOf(0));
      fSchedueTemplate.setTue(Integer.valueOf(0));
      fSchedueTemplate.setWed(Integer.valueOf(0));
      fSchedueTemplate.setThu(Integer.valueOf(0));
      fSchedueTemplate.setFri(Integer.valueOf(0));
      fSchedueTemplate.setSat(Integer.valueOf(0));
      fSchedueTemplate.setSun(Integer.valueOf(0));
    } 
    model.addAttribute("fSchedueTemplate", fSchedueTemplate);
    return "modules/phy/fSchedueTemplateForm";
  }
  
  @RequiresPermissions({"phy:fSchedueTemplate:edit"})
  @RequestMapping({"save"})
  public String save(FSchedueTemplate fSchedueTemplate, Model model, RedirectAttributes redirectAttributes) {
    if (fSchedueTemplate != null && StringUtils.isBlank(fSchedueTemplate.getId())) {
      List<FSchedueTemplate> list = this.fSchedueTemplateService.findList(new FSchedueTemplate());
      if (list.size() >= 5) {
        addMessage(redirectAttributes, new String[] { "创建失败，班次最多创建5个" });
        return "redirect:" + Global.getAdminPath() + "/phy/fSchedue/?repage";
      } 
    } 
    if (!beanValidator(model, fSchedueTemplate, new Class[0]))
      return form(fSchedueTemplate, model); 
    fSchedueTemplate.setTenantId(Integer.valueOf(5));
    this.fSchedueTemplateService.save(fSchedueTemplate);
    addMessage(redirectAttributes, new String[] { "保存排班模板成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/fSchedueTemplate/?repage";
  }
  
  @RequiresPermissions({"phy:fSchedueTemplate:edit"})
  @RequestMapping({"delete"})
  public String delete(FSchedueTemplate fSchedueTemplate, RedirectAttributes redirectAttributes) {
    this.fSchedueTemplateService.delete(fSchedueTemplate);
    addMessage(redirectAttributes, new String[] { "删除排班模板成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/fSchedueTemplate/?repage";
  }
}
